// pages/Sign/phonden.js
import {
  LoginModel
} from "./LoginModel";
var app = getApp()
const loginModel = new LoginModel();
var utils = require('../../utils/util.js');
var intervalId
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isAgreePrivacy: false, // 是否同意了《隐私政策》
    isAgreeMessage: false, // 是否同意了接受Howell花冠产品及活动信息
    phone: '', // 手机号
    downSecond: 0, // 倒计时时间
    code: '', //验证码
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    // 取消倒计时
    if (intervalId) {
      clearInterval(intervalId)
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 选择隐私政策
   * @param e
   */
  onCheckAgreePrivacy: function (e) {
    let that = this
    this.setData({
      isAgreePrivacy: !that.data.isAgreePrivacy
    })
  },
  /**
   * 选择接受Howell花冠产品及活动信息
   * @param e
   */
  onCheckAgreeMessage: function (e) {
    let that = this
    this.setData({
      isAgreeMessage: !that.data.isAgreeMessage
    })
  },
  /**
   * 绑定手机号
   */
  bindPhone: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },
  /**
   * 点击获取验证码
   */
  onClickGetCode: function (e) {
    let that = this;

    function showToast(info) {
      wx.showToast({
        title: info,
        icon: "none"
      })
    }
    if (!that.data.isAgreePrivacy || !that.data.isAgreeMessage) {
      showToast("请同意条款");
      return
    }
    if (!that.data.phone) {
      showToast("请输入手机号");
      return
    }
    if (that.data.downSecond != 0) {
      return
    }
    app.request("login/getsmscode", {
      mobile: that.data.phone,
      type: "login"
    }, res => {
      that.startDownTime(res.data.second)
    })
  },
  /**
   * 绑定验证码
   */
  bindCode: function (e) {
    this.setData({
      code: e.detail.value
    })
  },

  /**
   * 开始倒计时
   * @param sencond 倒计时时间
   */
  startDownTime(second) {
    let that = this
    if (that.data.downSecond != 0) {
      return
    }
    that.setData({
      downSecond: second
    })
    intervalId = setInterval(function () {
      second = second - 1;
      that.setData({
        downSecond: second
      })
      if (second == 0) {
        that.setData({
          downSecond: 0
        })
        clearInterval(intervalId)
        intervalId = ''
      }
    }, 1000)
  },
  /**
   * 登录
   * @param e
   */
  onClickComplete: function (e) {
    let that = this;
    loginModel.loginWithCode(that.data.phone, that.data.code, function (e) {
      wx.navigateBack({
        delta: 2
      })
    })
  }
})